<template>
  <div class="login-container">
    <div class="picture" />
    <el-form

      ref="loginFormRef"
      :model="loginForm"
      :rules="loginFormRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">

        <span>用户登录</span>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button
        class="login-btn"
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >登录</el-button>

    </el-form>
  </div>
</template>

<script>

import Shajs from 'jssha'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'root@admin.com',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
          { min: 6, max: 16, message: '密码必须是6-16位', trigger: ['blur', 'change'] }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = 'text'
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      const shajs = new Shajs('SHA-256', 'TEXT', { encoding: 'UTF8' })
      shajs.update(this.loginForm.password)

      // 整体校验
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        // 调接口发请求
        await this.$store.dispatch('user/loginAction', { username: this.loginForm.username, password: shajs.getHash('HEX') })
        this.$message.success('登陆成功')
        this.$router.push('/')
      })
    }

  }
}
</script>

<style lang="scss" scoped>
// 样式变量
$bg: #283443;
$dark_gray: #889aa4;
$light_gray: #407ffe;
$cursor:#fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $light_gray;
  }
}

// 登录容器
.login-container {
position: relative;
  background: no-repeat url('~@/assets/common/bg1.jpg'); // 设置背景图片
background-size: 100% 100%;
  height: 100%;
  width: 100%;
  .picture{
    width: 100%;
  height: 100%;
  background: no-repeat url('~@/assets/common/bgsmall.png');
  background-position: 100% 100%;

  }
  .el-form{
  width: 520px;
  padding: 30px;
  background-color:transparent;
  position: absolute;
  top: 50%;
  left: 40%;
  transform: translate(-50%, -50%, );
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $light_gray;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .login-form {

    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    .login-btn {
      background: #407ffe;
      height: 64px;
      line-height: 32px;
      font-size: 24px;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    display: flex;
   justify-content:space-between;
   margin-bottom: 10px;
   font-size: 20px;
   color: #fff;

  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.9); // 输入登录表单的背景色
    border-radius: 5px;
    color: $light_gray;
    .el-form-item__error {
      color: #fff;
      font-size: 14px;
    }
  }
}
</style>
